<template>

<el-main>
<el-row>
<el-col :span="22" :offset="1">
<el-tabs type="border-card">
<el-tab-pane label="用户管理">

<el-row >
    <el-col :span="8" :offset="5">
        <el-form :model="formData" size="mini" ref="ftpform" label-width="180px" :validate-on-rule-change="false">
          <el-form-item label="用户名" prop="types">
            <el-input v-model="formData.name" :disabled="namedis"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="types">
            <el-input v-model="password" :disabled="namedis" show-password></el-input>
          </el-form-item>
          <el-form-item label="登录IP白名单" prop="iplist">
            <ul v-for="item in formData.iplist" :key="item">
              <li>{{item}}</li>
            </ul>
            <el-input v-model="newip">
              <el-button slot="append" @click="addip">提交</el-button>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-if="$route.query.id == ''" @click="submitAdd">确定添加</el-button>
            <el-button type="primary" v-else @click="submitEdit">确定修改</el-button>
          </el-form-item>
        </el-form>
    </el-col>
</el-row>

    </el-tab-pane>
</el-tabs>
</el-col>
</el-row>
</el-main>

</template>
<script>
export default {
  data() {
    return {
      formData: { password: "", iplist: [] },
      newip: "",
      namedis: false,
      password: "",
    };
  },
  created() {
    if (this.$route.query.id && this.$route.query.id !== "") {
      this.namedis = true;
      this.$http.Get(this.$api.user + "/" + this.$route.query.id, {}, (res) => {
        if (res.iplist === null) {
          res.iplist = [];
        }
        this.formData = res;
      });
    } else {
      this.namedis = false;
    }
  },
  methods: {
    addip() {
      this.formData.iplist.push(this.newip);
    },
    submitAdd() {
      if (this.password !== "") {
        this.formData.password = this.$md5(this.password);
      }
      this.$http.Post(this.$api.user, this.formData, () => {
        this.$emit("close");
      });
    },
    submitEdit() {
      this.$http.Put(
        this.$api.user + "/" + this.$route.query.id,
        this.formData,
        () => {
          this.$emit("close");
        }
      );
    },
  },
};
</script>